package com.tianhui.consignortwofei.mvp.ui.viewHolder.detail;

import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;

import com.fgs.common.utils.CommonUtil;
import com.fgs.common.widget.itemView.InputItemView;
import com.tianhui.consignortwofei.R;
import com.tianhui.consignortwofei.mvp.model.enty.detailItem.DetailFreightInfo;
import com.tianhui.consignortwofei.mvp.ui.adapter.DetailAdapter;
import com.tianhui.consignortwofei.pattern.freightCalculation.CalculationController;

import org.yczbj.ycrefreshviewlib.holder.BaseViewHolder;

/**
 * @author fgs
 * @date 2020/9/8
 * @description
 **/
public class DetailFreightViewHolder extends BaseViewHolder<DetailFreightInfo> {
    private InputItemView goodsPriceInputItemView;
    private InputItemView basePriceInputItemView;
    private InputItemView grantPriceInputItemView;
    private InputItemView deductPriceInputItemView;
    private InputItemView roadLoseInputItemView;
    private InputItemView inptiv_cangchu;
    private InputItemView inptiv_zhuangche;

    private InputItemView inptiv_huowu_name;


    private EditText grantsRemarkEditText;
    private EditText deductRemarkEditText;
    private String goodsPrice = "";
    private String price = "";
    private String grants = "";
    private String otherPrice = "";
    private String grantsRemark;
    private String deductRemark;

    private String zhuangChePrice = "0";
    private String cangChuPrice = "0";




    public DetailFreightViewHolder(ViewGroup parent, int res, DetailAdapter.OnFreightInputItemListener onFreightInputItemListener) {
        super(parent, res);
        goodsPriceInputItemView = getView(R.id.item_detail_freight_goodsPriceInputItemView);
        basePriceInputItemView = getView(R.id.item_detail_freight_basePriceInputItemView);
        grantPriceInputItemView = getView(R.id.item_detail_freight_grantPriceInputItemView);
        deductPriceInputItemView = getView(R.id.item_detail_freight_deductPriceInputItemView);
        roadLoseInputItemView = getView(R.id.item_detail_freight_roadLoseInputItemView);
        grantsRemarkEditText = getView(R.id.item_detail_freight_grantMarkEditText);
        deductRemarkEditText = getView(R.id.item_detail_freight_deductMarkEditText);

        inptiv_huowu_name = getView(R.id.inptiv_huowu_name);


        inptiv_cangchu = getView(R.id.inptiv_cangchu);
        inptiv_zhuangche = getView(R.id.inptiv_zhuangche);
        initListener(onFreightInputItemListener);
    }

    private void initListener(DetailAdapter.OnFreightInputItemListener onFreightInputItemListener) {

        inptiv_cangchu.setOnInputItemListener(new InputItemView.OnInputItemListener() {
            @Override
            public void onInput(String string) {
                zhuangChePrice = string;
                if (onFreightInputItemListener != null) {
                    onFreightInputItemListener.onInput(goodsPrice, price, grants, otherPrice,zhuangChePrice,cangChuPrice);
                }
            }
        });

        inptiv_zhuangche.setOnInputItemListener(new InputItemView.OnInputItemListener() {
            @Override
            public void onInput(String string) {
                cangChuPrice = string;
                if (onFreightInputItemListener != null) {
                    onFreightInputItemListener.onInput(goodsPrice, price, grants, otherPrice,zhuangChePrice,cangChuPrice);
                }
            }
        });

        goodsPriceInputItemView.setOnInputItemListener(new InputItemView.OnInputItemListener() {
            @Override
            public void onInput(String string) {
                goodsPrice = string;
                if (onFreightInputItemListener != null) {
                    onFreightInputItemListener.onInput(goodsPrice, price, grants, otherPrice,zhuangChePrice,cangChuPrice);
                }
            }
        });
        basePriceInputItemView.setOnInputItemListener(new InputItemView.OnInputItemListener() {
            @Override
            public void onInput(String string) {
                price = string;
                if (onFreightInputItemListener != null) {
                    onFreightInputItemListener.onInput(goodsPrice, price, grants, otherPrice,zhuangChePrice,cangChuPrice);
                }
            }
        });
        grantPriceInputItemView.setOnInputItemListener(new InputItemView.OnInputItemListener() {
            @Override
            public void onInput(String string) {
                grants = string;
                if (onFreightInputItemListener != null) {
                    onFreightInputItemListener.onInput(goodsPrice, price, grants, otherPrice,zhuangChePrice,cangChuPrice);
                }
            }
        });
        deductPriceInputItemView.setOnInputItemListener(new InputItemView.OnInputItemListener() {
            @Override
            public void onInput(String string) {
                otherPrice = string;
                if (onFreightInputItemListener != null) {
                    onFreightInputItemListener.onInput(goodsPrice, price, grants, otherPrice,zhuangChePrice,cangChuPrice);
                }
            }
        });

        grantsRemarkEditText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (onFreightInputItemListener != null) {
                    grantsRemark = s.toString();
                    onFreightInputItemListener.onRemarkInput(grantsRemark, deductRemark);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {
            }
        });


        deductRemarkEditText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (onFreightInputItemListener != null) {
                    deductRemark = s.toString();
                    onFreightInputItemListener.onRemarkInput(grantsRemark, deductRemark);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });




    }





    @Override
    public void setData(DetailFreightInfo data) {
        super.setData(data);
        goodsPriceInputItemView.setContent(data.goodsPrice);
        basePriceInputItemView.setContent(data.tradprice);
        grantPriceInputItemView.setContent(data.grants);
        deductPriceInputItemView.setContent(data.otherPrice);
        String consume = CalculationController.getInstance().getReasonableWeight(data.orderInfo) + "";
        roadLoseInputItemView.setContent(CommonUtil.keepTwoDecimal(consume));

        if (null!=data.goodsdescription&&!"".equals(data.goodsdescription)) {
            inptiv_huowu_name.setVisibility(View.VISIBLE);
            inptiv_huowu_name.setContent(data.goodsdescription);
        }else{
            inptiv_huowu_name.setVisibility(View.GONE);
        }

    }

}
